/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package JavaAssignment;

import java.io.*;
import java.util.*;

/**
 *
 * @author Ishaan
 */
public class GameCollection implements Serializable{

    private LinkList gameList;
    private int currGame;
    private int noOfElement = 0;
    private ListNode headnode = null;

    public GameCollection() {

        gameList = new LinkList();
        currGame = 0;
    }

    public int getNumOfGames() {
        return gameList.getNoOfElement();
    }

    public GameData getGame(int index) {
        return (GameData) gameList.get(index);
    }

    public void addGame(GameData e) {
        gameList.addLast(e);
    }

    public int getNext() {
        if (currGame < gameList.getNoOfElement() - 1) {
            currGame++;
        }
        return currGame;
    }

    public int getPrev() {
        if (currGame > 0) {
            currGame--;
        }
        return currGame;

    }

    public int getFirst() {
        currGame = 0;
        return currGame;

    }

    public int getLast() {

        currGame = gameList.getNoOfElement() - 1;
        return currGame;
    }

    public void remove(int index) throws IndexOutOfBoundsException {
        if (currGame >= 0 && currGame <= noOfElement) {
            if (currGame == 0) {
                headnode = headnode.getNext();
            } else {
                ListNode prevnode = find(index - 1);
                ListNode currnode = prevnode.getNext();
                prevnode.setNext(currnode.getNext());
            }
            noOfElement--;
        } else {
            throw new IndexOutOfBoundsException(
                    "index out of bounds exception on remove");
        }
    }

    private ListNode find(int index) {
        ListNode currnode = headnode;
        for (int i = 0; i < index; i++) {
            currnode = currnode.getNext();
        }
        return currnode;
    }

    public void add(int index, Object data) throws IndexOutOfBoundsException {

        if (index >= 0 && index <= noOfElement + 1) {
            if (index == 0) {
                ListNode newnode = new ListNode(data, headnode);
                headnode = newnode;
            } else {
                ListNode prevnode = find(index - 1);
                ListNode newnode = new ListNode(data, prevnode.getNext());
                prevnode.setNext(newnode);
            }
            noOfElement++;
        } else {
            throw new IndexOutOfBoundsException(
                    "index out of bounds exception on add");
        }
    }
}


